package org.breizhjug.cxf2jmeter;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.List;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;

public class JMeterWriter
{
    private File file;

    private VelocityEngine engine;

    public JMeterWriter( File file )
    {
        this.file = file;

        engine = new VelocityEngine();
        engine.setProperty( "resource.loader", "class" );
        engine.setProperty( "class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader" );
    }

    public void writeMessages( List<Message> messages )
        throws Exception
    {
        BufferedWriter writer = new BufferedWriter( new FileWriter( file ) );
        try
        {
            Context context = new VelocityContext();
            context.put( "messages", messages );
            engine.mergeTemplate( "org/breizhjug/cxf2jmeter/jMeterTemplate.vm", context, writer );
        }
        finally
        {
            writer.close();
        }
    }
}
